package com.uubx.service.impl;

import com.uubx.mapper.DepartmentMapper;
import com.uubx.mapper.UserMapper;
import com.uubx.pojo.Department;
import com.uubx.pojo.User;
import com.uubx.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author wuguojun
 * @Date 2020/12/26 18:53
 * @Version 1.0
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Department> queryAllDepartment(Integer startIndex, Integer limit) {
        List<Department> list = departmentMapper.queryAllDepartment(startIndex, limit);
        return list;
    }

    @Override
    public Long queryAllDepartmentCount() {
        Long count = departmentMapper.queryAllDepartmentCount();
        return count;
    }

    @Override
    public List<Department> queryDepartmentByName(String name) {
        List<Department> departments = departmentMapper.queryDepartmentByName(name);
        return departments;
    }

    @Override
    public Long queryDepartmentNameToCount(String name) {
        Long count = departmentMapper.queryDepartmentNameToCount(name);
        return count;
    }

    @Override
    public List<Department> queryDepartmentByStatus(Integer status) {
        List<Department> departments = departmentMapper.queryDepartmentByStatus(status);
        return departments;
    }

    @Override
    public Long queryDepartmentByStatusToCount(Integer status) {
        Long count = departmentMapper.queryDepartmentByStatusToCount(status);
        return count;
    }

    @Override
    public List<User> queryAllUsersByDepartmentId(Integer departmentId) {
        List<User> users = userMapper.queryAllUsersByDepartmentId(departmentId);
        return users;
    }

    @Override
    public Long queryAllUsersByDepartmentIdToCount(Integer departmentId) {
        Long count = userMapper.queryAllUsersByDepartmentIdToCount(departmentId);
        return count;
    }

    /**
     * 通过id修改状态
     * @param
     */
    @Override
    public void updateDepartmentById(Department department) {
        if (department.getStatus()==1){
            department.setStatus(0);
            departmentMapper.updateDepartmentById(department.getId(),department.getStatus());
        }else {
            department.setStatus(1);
            departmentMapper.updateDepartmentById(department.getId(),department.getStatus());
        }

    }

    @Override
    public Department queryDepartmentById(Integer id) {
        Department department = departmentMapper.queryDepartmentById(id);
        return department;
    }

    @Override
    public void deleteDepartmentById(Integer id) {
        departmentMapper.deleteDepartmentById(id);
    }

    @Override
    public List<String> queryAllDepartmentName() {
        List<String> stringList = departmentMapper.queryAllDepartmentName();
        return stringList;
    }

    @Override
    public void addDepartment(Department department) {
        department.setCreateTime(new Date());
        departmentMapper.addDepartment(department);
    }
}
